$(function () {
    //获取url
    var url = localStorage.getItem("ajaxUrl");
    var pass = 1;
    // var productInWh = localStorage.getItem("productInWh");
    var productInDp = localStorage.getItem("productInDp");

    $(".head .icon-lishi1").on("click", function () {
        window.location.href = "product_lishi.html";
    });

    $("#goods_id").focus();

    //搜索 批号
    $("#goods_id").on("keyup", function (event) {
        if (event.keyCode == 13) {
            if (pass == 0) {
                return
            }
            pass = 0;
            var val = $(this).val();
            if (!val) {
                pass = 1;
                $alert("批号不可为空！");
                return
            }
            if ($(".content .list-block .goodsList_ul li[attr-data='" + val + "']").length == 1) {
                $alert("不可重复扫描");
                pass = 1;
                setTimeout(function () {
                    $("#goods_id").val("").focus();
                }, 1400);
                return
            }
            var pack_con = JSON.parse(localStorage.getItem("pack_con"));
            var firstBatch = "";
            if (pack_con && pack_con[0]) {
                firstBatch = pack_con[0].cpph;
            }
            // if (pack_con && pack_con.length > 20) {
            //     $("input.cpph").val("");
            //     pass = 1;
            //     return $alert("已达到20件了，请即时提交！");
            // }
            //获取 产品信息  在wd 中
            loading_show("搜索中...");
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "POST",
                url: url + "/app/productinorder/getWareouseForProduct",
                data: {
                    batch: val,
                    firstBatch: firstBatch
                },
                success: function (res) {
                    pass = 1;
                    loading_hide();
                    if (res.success) {
                        var data = res.result;
                        if (!data) {
                            $alert("此产品不存在！");
                            $("#goods_id").val("").focus();
                        } else {
                            localStorage.setItem("oneProductData", JSON.stringify(data));
                            window.location.href = "product.html";
                        }
                    } else {
                        console.log(res);
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    pass = 1;
                    loading_hide();
                    console.log(error);
                    $alert(error.responseText);
                }
            });
        }
    });

    //获取 localStorage.getItem("pack_con") 生成  打包 数据
    if (localStorage.getItem("pack_con")) {
        $(".content .list-block .goodsList_ul").empty();
        var pack_html = "";
        var pack_con = JSON.parse(localStorage.getItem("pack_con"));
        for (var i = 0; i < pack_con.length; i++) {
            pack_html += '<li attr-data = "' + pack_con[i].cpph + '"><div class="row no-gutter">' +
                '<div class="col-90"><p class="p_none"><span>打包日期:</span><span class="sp goods_dbrq">' +
                pack_con[i].dbrq + '</span><span>皮重:</span><span class="sp goods_cppz">' +
                pack_con[i].cppz + '</span><span>编码:</span><span class="sp goods_cpbm">' +
                pack_con[i].cpbm + '</span><span>客户:</span><span class="sp goods_cpkh">' +
                pack_con[i].cpkh + '</span><span>alias:</span><span class="sp goods_alias">' +
                pack_con[i].alias + '</span><span>强度:</span><span class="sp goods_cpqd">' +
                pack_con[i].cpqd + '</span><span>锌层:</span><span class="sp goods_cpxc">' +
                pack_con[i].cpxc + '</span><span>炉号:</span><span class="sp goods_cplh">' +
                pack_con[i].cplh + '</span><span>生产日期:</span><span class="sp goods_cprq">' +
                pack_con[i].cprq + '</span><span>订单类型:</span><span class="sp goods_cplx">' +
                pack_con[i].cplx + '</span></p>' +
                '<p><span>名称:</span><span class="sp goods_cpmc">' +
                pack_con[i].cpmc + '</span></p><p><span>规格:</span><span class="sp goods_ggxh">' +
                pack_con[i].ggxh + '</span><span>库位:</span><span class="sp goods_cpkw">' +
                pack_con[i].cpkw + '</span></p><p><span>排产单:</span><span class="sp goods_cppcd">' +
                pack_con[i].cppcd + '</span></p><p><span>客户简称:</span><span class="sp goods_cpkh">' +
                pack_con[i].cpkh + '</span></p><p><span>批号:</span><span class="sp goods_cpph">' +
                pack_con[i].cpph + '</span></p><p><span>净重:</span><span class="sp goods_cpjz">' +
                pack_con[i].cpjz + '</span><span>T 毛重:</span><span class="sp goods_cpmz">' +
                pack_con[i].cpmz + '</span> T</p><p><span>实测直径:</span><span class="sp goods_cpsczj">' +
                pack_con[i].cpsczj + '</span><span>结果:</span><span class="sp goods_cpsczjjg">' +
                pack_con[i].cpsczjjg + '</span></p></div>' +
                '<div class="col-10"><span class="icon iconfont icon-msnui-jian pull-right"></span>' +
                '<span class="icon iconfont icon-dayin pull-right"></span></div>' +
                '</div></li>';
        }
        $(".content .list-block .goodsList_ul").append(pack_html);
    }

    // li 中的 减号 点击 删除 本条
    $(".content").on("click", ".goodsList_ul li .col-10 .icon-msnui-jian", function () {
        var jianhao = $(this);
        $.confirm('确定删除此货品？', function () {
            jianhao.parent().parent().parent().remove();
            badge_text();
            setPackCon();
        });
    });

    // li 中的 打印 点击 打印 本条
    $(".content").on("click", ".goodsList_ul li .col-10 .icon-dayin", function () {
        var li = $(this).parent().parent().parent();
        var number = li.find("span.goods_cpph").text();
        // var cpmc = li.find("span.goods_cpmc").text();
        // var ggxh = li.find("span.goods_ggxh").text();
        // var cpqd = li.find("span.goods_cpqd").text();
        var cpxc = li.find("span.goods_cpxc").text();
        // var cplh = li.find("span.goods_cplh").text();
        // var cpmz = li.find("span.goods_cpmz").text();
        var cpjz = li.find("span.goods_cpjz").text();
        // var cprq = li.find("span.goods_cprq").text();
        // var cplx = li.find("span.goods_cplx").text();
        // var cpkh = li.find("span.goods_cpkh").text();
        //console.log(number)
        //打印标签
        $.ajax({
            headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
            type: "post",
            url: url + '/app/productinorder/printWarehouseNumberForProduct',
            data: {
                number: number
            },
            dataType: 'json',
            json: 'callback',
            success: function (res) {
                if (res.success) {
                    var data = res.result;
                    $alert(number + "打印成功");
                    //打印钢丝成品标签
                    data.zincLayerWeight = cpxc;
                    data.quantity = cpjz;
                    data.availableQuantity = cpjz;
                    takeupPrint("wireProduct", data);
                } else {
                    $alert(res.message);
                }
            },
            error: function (error) {
                console.log(error);
                loading_hide();
                $alert(error.responseText);
            }
        });
    });

    //点击 继续添加 跳转 到 pack.html
    $(".foot").on("click", ".add_go", function () {
        setPackCon();
        window.location.href = "product.html";
    });

    // 点击 添加完成 生成 产成品入库单
    $(".foot").on("click", ".add_success", function () {
        if ($(".content .goodsList_ul li").length <= 0) {
            return $alert("请添加入库产品！");
        } else {
            $.confirm("确认" + $(".content .goodsList_ul li").length + "件产品入库吗？", function () {
                if (pass == 0) {
                    return
                }
                pass = 0;
                loading_show("提交中...");
                //获取 单据信息
                setPackCon();
                var arr_goods_list = JSON.parse(localStorage.getItem("pack_con"));
                var data = [];
                for (var i = 0; i < arr_goods_list.length; i++) {
                    data.push({
                        number: arr_goods_list[i].cpph,
                        weight: arr_goods_list[i].cpmz,
                        tare_weight: arr_goods_list[i].cppz,
                        net_weight: arr_goods_list[i].cpjz,
                        location: arr_goods_list[i].cpkw,
                        cpqd: arr_goods_list[i].cpqd,
                        cpxc: arr_goods_list[i].cpxc
                    })
                }
                //整合数据
                $.ajax({
                    headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                    type: "POST",
                    url: url + "/app/productinorder/saveProductInOrder",
                    data: {
                        // whCode: productInWh.split(",")[1],
                        // dpCode: productInDp.split(",")[1],
                        data: JSON.stringify(data),
                    },
                    timeout: 60000,
                    success: function (res) {
                        pass = 1;
                        loading_hide();
                        if (res.success) {
                            console.log("产成品数据录入成功");
                            localStorage.removeItem("pack_con");
                            localStorage.removeItem("model");
                            localStorage.removeItem("oneProductData");
                            localStorage.removeItem("productInWh");
                            localStorage.removeItem("productInDp");
                            window.location.href = "storekeeper.html";
                        } else {
                            $alert(res.message);
                        }
                    },
                    error: function (error) {
                        if (error.status == 0) {
                            pass = 1;
                            loading_hide();
                            $alert("请求超时，请重新操作！");
                        } else {
                            pass = 1;
                            loading_hide();
                            console.log("产成品数据录入失败");
                            $alert(error.responseText);
                        }
                    }
                });
            });

            // var jth = $(".content .goodsList_ul li").eq(0).find("span.goods_cpjth").html();
            // $(".mengceng").show();
            // $("div.div_ruku").show();
        }
    });

    //设置缓存
    function setPackCon() {
        var pack_con = $(".content .list-block .goodsList_ul li");
        var all_goods = [];
        for (var i = 0; i < pack_con.length; i++) {
            var dbrq = pack_con.eq(i).find(".row span.goods_dbrq").html();
            var cpjz = pack_con.eq(i).find(".row span.goods_cpjz").html();
            var ggxh = pack_con.eq(i).find(".row span.goods_ggxh").html();
            var cpph = pack_con.eq(i).find(".row span.goods_cpph").html();
            var cpmz = pack_con.eq(i).find(".row span.goods_cpmz").html();
            var cppz = pack_con.eq(i).find(".row span.goods_cppz").html();
            var cpbm = pack_con.eq(i).find(".row span.goods_cpbm").html();
            var cpmc = pack_con.eq(i).find(".row span.goods_cpmc").html();
            var cppcd = pack_con.eq(i).find(".row span.goods_cppcd").html();
            var cpkh = pack_con.eq(i).find(".row span.goods_cpkh").html();
            var cpkw = pack_con.eq(i).find(".row span.goods_cpkw").html();
            var cplx = pack_con.eq(i).find(".row span.goods_cplx").html();
            var alias = pack_con.eq(i).find(".row span.goods_alias").html();
            var cprq = pack_con.eq(i).find(".row span.goods_cprq").html();
            var cpqd = pack_con.eq(i).find(".row span.goods_cpqd").html();
            var cpxc = pack_con.eq(i).find(".row span.goods_cpxc").html();
            var cplh = pack_con.eq(i).find(".row span.goods_cplh").html();
            var cpsczj = pack_con.eq(i).find(".row span.goods_cpsczj").html();
            var cpsczjjg = pack_con.eq(i).find(".row span.goods_cpsczjjg").html();
            all_goods.push({
                "dbrq": dbrq,
                "cpjz": cpjz,
                "ggxh": ggxh,
                "cplx": cplx,
                "cpph": cpph,
                "cpmz": cpmz,
                "cppz": cppz,
                "cpbm": cpbm,
                "cpmc": cpmc,
                "cppcd": cppcd,
                "cpkh": cpkh,
                "cpkw": cpkw,
                "cprq": cprq,
                "cpqd": cpqd,
                "cpxc": cpxc,
                "cplh": cplh,
                "cpsczj": cpsczj,
                "cpsczjjg": cpsczjjg,
                "alias": alias
            });
        }
        //重设 缓存
        localStorage.setItem("pack_con", JSON.stringify(all_goods));
    }

    //点击 某一条 数据  改变 背景颜色
    $(".content").on("click", ".goodsList_ul li", function () {
        $(this).css("background", "#cccccc").siblings("li").css("background", "#ffffff");
    });

    // goodsList中 badge 的值 为 列表中 li 的个数
    function badge_text() {
        var badge_len = $(".content .goodsList_ul li").length;
        //console.log(badge_len);
        var allWeight = 0;
        for (var i = 0; i < badge_len; i++) {
            var weight = $(".content .goodsList_ul li").eq(i).find(".row span.goods_cpjz").html();
            allWeight = accAdd(allWeight, weight);
        }
        $(".details span.good_length").html(badge_len);
        $(".details span.good_weight").html(allWeight);
    }

    badge_text();



});